package dao;

import util.DbUtil;
import java.sql.*;

import model.User;

public class UserDao {
	
	//直接添加用户
	public int addUser(User user){
	    int flag=0;
		DbUtil dbutil=new DbUtil();
		Connection con = dbutil.dbcon();
		String sqls="insert into user (userName) value (?)";
		PreparedStatement ps=null;
		try {
			ps=con.prepareStatement(sqls);
			ps.setString(1,user.getUserName() );
			flag=ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		dbutil.closecon(ps, null);
		return flag;
	}
	
	//通过指定id添加用户
	public int addUserById(User user){
	    int flag=0;
		DbUtil dbutil=new DbUtil();
		Connection con = dbutil.dbcon();
		String sqls="insert into user (userId,userName) value (?,?)";
		PreparedStatement ps=null;
		try {
			ps=con.prepareStatement(sqls);
			ps.setInt(1, user.getUserId());
			ps.setString(2,user.getUserName() );
			flag=ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		dbutil.closecon(ps, null);
		return flag;
	}
	
	//通过id删除用户
	public int delUserById(int userId){
		DbUtil dbutil=new DbUtil();
		Connection con = dbutil.dbcon();
		String sqls="delete from user (userId) value (?,)";
		PreparedStatement ps=null;
		try {
			ps=con.prepareStatement(sqls);
			ps.setInt(1, userId);
			userId=ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		dbutil.closecon(ps, null);
		return userId;	
	}	
}
